﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPublisher.Web.Framework;
//using VietPublisher.Membership;
using VietPublisher.BSL.BK;
using VietPublisher.Repositories.Interface;
using VietPublisher.BaseProject;
using VietPublisher.BaseProject.Models;

namespace VietPublisher.Web.BK.Controllers.Admin
{
    public class AccountController : StringBaseEntityController
    {

        // set repository

      


        //
        // GET: /Account/

        public ActionResult Index()
        {
            IAuthorRepository authors = new AuthorRepository();
            authors.GetAuthorList();
            return View();
        }

        [Authorize]
        public ActionResult AccountList()
        {
            List<T_BK_UserExtend> users= RepoAccount.getAccountList();
            return View("List_Account",users);
        }


        #region account modify

        //
        // GET: /Account/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Account/Create

        public ActionResult Create()
        {
            return View("Edit_Account");
        }

        //
        // POST: /Account/Create

        [HttpPost]
        public ActionResult ActionCreate(FormCollection collection)
        {

          

            var result = false;
            try
            {
                // TODO: Add insert logic here
                //[triet] [2012-06-06 9:26 PM] [create user]
                T_BK_UserExtend User = new T_BK_UserExtend()
                {
                    Address = Request.Form["Address"] != null ? Request.Form["Address"].ToString() : "",
                    BirthDate = Request.Form["BirthDate"] != string.Empty  ? DateTime.Parse(Request.Form["BirthDate"]) : DateTime.MinValue.AddYears(1900),
                    Email = Request.Form["Email"] != null ? Request.Form["Email"].ToString() : "",
                    IsMale = Request.Form["IsMale"] != null ? bool.Parse(Request.Form["IsMale"]) : true,
                    MobilePhone = Request.Form["MobilePhone"] != null ? Request.Form["MobilePhone"].ToString() : "",
                    Name = Request.Form["Name"] != null ? Request.Form["Name"].ToString() : "",
                    UpdateDate = DateTime.Now,
                    UserName = Request.Form["UserName"] != null ? Request.Form["UserName"].ToString() : "",
                    UserTypes = (int)Enums.UserTypes.CUSTOMER,
                    UseYN = true,

                };
                string Password = Request.Form["Password"] != null ? Request.Form["Password"].ToString() : "";
                
               result= RepoAccount.CreateAccount(User, Password,Enums.UserTypes.CUSTOMER);

                return Content(result.ToString());
            }
            catch
            {
                return Content(result.ToString());
            }
        }

        //
        // GET: /Account/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Account/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Account/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Account/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult ChangePassword(FormCollection collection) {
            bool result = false;
            try
            {
                string userName = Request.Form["userName"] != null ? Request.Form["userName"].ToString() : "";
                string oldPass = Request.Form["oldPass"] != null ? Request.Form["oldPass"].ToString() : "";
                string newPass = Request.Form["newPass"] != null ? Request.Form["newPass"].ToString() : "";
                RepoAccount.ChangePassword(userName, oldPass, newPass);
                result = true;
            }
            catch (Exception e)
            {
                FileManager.LogUtility.LogMessage("Account", "ChangePassword", e.Message);
                result = false;
            }
            return Content(result.ToString());
        
        }


        #endregion
    }
}
